package com.choosefine.it.team.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.choosefine.it.base.SendPush;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.basedata.model.UserInfo;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.exception.BusinessException;
import com.choosefine.it.team.dao.TeamFindProjectInfoMapper;
import com.choosefine.it.team.model.TeamFindProjectInfo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("teamFindProjectInfoService")
public class TeamFindProjectInfoService {
	@Autowired
	private TeamFindProjectInfoMapper teamFindProjectInfoMapper;

	@Autowired
	private MongoTemplate mongoTemplate;
	
	@Autowired
	private SendPush sendPush;
	
	public Long addTeamFindProjectInfo(TeamFindProjectInfo teamFindProjectInfo) throws BusinessException {
		long isNum = 0;
		UserInfo userInfo=mongoTemplate.findOne(new Query(Criteria.where("userCode").is(teamFindProjectInfo.getUserCode())),UserInfo.class);
		if (userInfo != null){
			if (StringUtils.isNotBlank(teamFindProjectInfo.getPublisher())) {
				teamFindProjectInfo.setVerifyStatus(CommonConstant.VERIFY_STATUS_1);
			} else {
				teamFindProjectInfo.setVerifyStatus(CommonConstant.VERIFY_STATUS_0);
			}
			teamFindProjectInfo.setReleaseNo("TFP"+System.currentTimeMillis());
			teamFindProjectInfo.setCtime(System.currentTimeMillis());
			teamFindProjectInfo.setPublishTime(System.currentTimeMillis());
			teamFindProjectInfo.setUserInfo(userInfo);/*yuxin3.6*/
			isNum = teamFindProjectInfoMapper.insertSelective(teamFindProjectInfo);
		}
		if(isNum>0){
			mongoTemplate.insert(teamFindProjectInfo);
		}
		return isNum;
	}

	
	public Page<TeamFindProjectInfo> getTeamFindProjectInfoList(Map<String, Object> map, PageDTO pageDTO)
			throws BusinessException {
		// 分页
		PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
		Page<TeamFindProjectInfo> page = teamFindProjectInfoMapper.selectByUserId(map);
//		for (int i = 0; i < page.size(); i++) {
//			page.set(i, mongoTemplate.findOne(new Query(Criteria.where("_id").is(page.get(i).getId())), TeamFindProjectInfo.class));
//		}
		return page;
	}

	
	public int updateFindProjectInfoPublishstatusByID(Map<String, Object> params) {
		int num = teamFindProjectInfoMapper.updateFindProjectPublishstatusByID(params);
		if(num > 0){
			Update update = new Update();
			if (params.containsKey("publishStatus")) {
				update.set("publishStatus", params.get("publishStatus"));
			}
			update.set("utime", params.get("utime"));
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(params.get("id"))),update , TeamFindProjectInfo.class);
		}
		return num;
	}

	
	public int deleteTeamFindProjectInfo(Map<String, Object> params) {
		int num = teamFindProjectInfoMapper.updateIsvalidStatus(params);
		if(num > 0){
			Update update = new Update();
			if (params.containsKey("isValid")) {
				update.set("isValid", params.get("isValid"));
			}
			update.set("utime", params.get("utime"));
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(params.get("id"))),update , TeamFindProjectInfo.class);
		}
		return num;
	}

	
	public TeamFindProjectInfo getTeamFindProjectInfoDetail(Integer id) {
		return mongoTemplate.findById(id, TeamFindProjectInfo.class);
	}

	
	public int updateTeamFindProjectInfo(TeamFindProjectInfo teamFindProjectInfo) {
		teamFindProjectInfo.setVerifyStatus(CommonConstant.VERIFY_STATUS_0);
		teamFindProjectInfo.setIsValid(CommonConstant.VALID);
		teamFindProjectInfo.setPublishTime(System.currentTimeMillis());
		teamFindProjectInfo.setUtime(System.currentTimeMillis());
		teamFindProjectInfo.setPublisher(null);
		int num = teamFindProjectInfoMapper.updateByPrimaryKeySelective(teamFindProjectInfo);
		if(num > 0){
			Update update = mongoUpdateTeamFindProjectInfo(teamFindProjectInfo);
			mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(teamFindProjectInfo.getId())),update , TeamFindProjectInfo.class);
		}
		return teamFindProjectInfoMapper.updateByPrimaryKeySelective(teamFindProjectInfo);
	}

	private Update mongoUpdateTeamFindProjectInfo(TeamFindProjectInfo teamRecruitmentInfo) {
		Update update = new Update();
		update.set("utime", teamRecruitmentInfo.getUtime());
		update.set("isValid", teamRecruitmentInfo.getIsValid());
		update.set("verifyStatus", teamRecruitmentInfo.getVerifyStatus());
		update.set("publisher", null);
		update.set("reason", null);
		if(!StringUtils.isEmpty(teamRecruitmentInfo.getProvinceCode())){
			update.set("provinceCode", teamRecruitmentInfo.getProvinceCode());
		}
		if(!StringUtils.isEmpty(teamRecruitmentInfo.getProvinceName())){
			update.set("provinceName", teamRecruitmentInfo.getProvinceName());
		}
		if(!StringUtils.isEmpty(teamRecruitmentInfo.getCityCode())){
			update.set("cityCode", teamRecruitmentInfo.getCityCode());
		}
		if(!StringUtils.isEmpty(teamRecruitmentInfo.getCityName())){
			update.set("cityName", teamRecruitmentInfo.getCityName());
		}
		if(!StringUtils.isEmpty(teamRecruitmentInfo.getDescribe())){
			update.set("describe", teamRecruitmentInfo.getDescribe());
		}
		if(null != teamRecruitmentInfo.getExpectedStartDate()) {
			update.set("expectedStartDate", teamRecruitmentInfo.getExpectedStartDate());
		}
		if(!StringUtils.isEmpty(teamRecruitmentInfo.getPhone())){
			update.set("phone", teamRecruitmentInfo.getPhone());
		}
		if(null != teamRecruitmentInfo.getEngCategoryId()){
			update.set("engCategoryId", teamRecruitmentInfo.getEngCategoryId());
		}
		if(!StringUtils.isEmpty(teamRecruitmentInfo.getPhone1())){
			update.set("phone1", teamRecruitmentInfo.getPhone1());
		}
		if(!StringUtils.isEmpty(teamRecruitmentInfo.getProjectTypeId())){
			update.set("projectTypeId", teamRecruitmentInfo.getProjectTypeId());
		}
		if(null != teamRecruitmentInfo.getPublishStatus()){
			update.set("publishStatus", teamRecruitmentInfo.getPublishStatus());
		}
		if(!StringUtils.isEmpty(teamRecruitmentInfo.getTeamContacts())){
			update.set("teamContacts", teamRecruitmentInfo.getTeamContacts());
		}
		if(!StringUtils.isEmpty(teamRecruitmentInfo.getTeamTypeId())){
			update.set("teamTypeId", teamRecruitmentInfo.getTeamTypeId());
		}
		if(!StringUtils.isEmpty(teamRecruitmentInfo.getTeamTypeName())){
			update.set("teamTypeName", teamRecruitmentInfo.getTeamTypeName());
		}
		if(!StringUtils.isEmpty(teamRecruitmentInfo.getProjectTypeName())){
			update.set("projectTypeName", teamRecruitmentInfo.getProjectTypeName());
		}
		return update;
	}

	
	public Page<TeamFindProjectInfo> selectByVerifyStatus(PageDTO pageDTO) {
		PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
		Page<TeamFindProjectInfo> page = teamFindProjectInfoMapper.selectByVerifyStatus();
		for (int i = 0; i < page.size(); i++) {
			page.set(i, mongoTemplate.findOne(new Query(Criteria.where("_id").is(page.get(i).getId())), TeamFindProjectInfo.class));
		}
		return page;
	}

	
	@Transactional
	public int updateFindProjectVerifyStatus(Map<String, Object> params) {
		int updateNum=0;
		try {
			updateNum= teamFindProjectInfoMapper.updateFindProjectVerifyStatus(params);
			if(updateNum>0){
//				//对用户进行推送审核结果的通知
//				ArrayList<Integer> idss=(ArrayList<Integer>) params.get("ids");
//				Integer verifyStatus= (Integer) params.get("verifyStatus");
//				for(Integer id:idss){
//					TeamFindProjectInfo teamFindProjectInfo = teamFindProjectInfoMapper.selectByPrimaryKey(id);
//				if(teamFindProjectInfo!=null){
//					String userCode = teamFindProjectInfo.getUserCode();
//					Assert.assertNotNull(userCode, "用户编号为空");
//					if(verifyStatus==1){
//						sendPush.sendPushByUsername(userCode, ConstantDef.MSG_TYPE_PUBLISH_ISVERIFY, ConstantDef.MSG_PUSH_PUBLISH_ISVERIFY);
//					}else{
//						sendPush.sendPushByUsername(userCode, ConstantDef.MSG_TYPE_PUBLISH_ISNOT_VERIFY, ConstantDef.MSG_PUSH_PUBLISH_ISNOT_VERIFY);
//					}
//				}
//				}
				Update update = new Update();
				if (params.containsKey("verifyStatus")) {
					update.set("verifyStatus", params.get("verifyStatus"));
				}
				if(params.containsKey("reason")){
					update.set("reason", params.get("reason"));
				}
				if(params.containsKey("publisher")){
					update.set("publisher", params.get("publisher"));
				}
				update.set("utime", params.get("utime"));
				mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(params.get("id"))),update , TeamFindProjectInfo.class);
			}
		}catch (Exception e){
 			e.printStackTrace();
		}
		return updateNum;
	}

	
	public Page<TeamFindProjectInfo> selectFindProjectInfoByCondition(Map<String, Object> params, PageDTO pageDTO) {
		PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
		Page<TeamFindProjectInfo> page = teamFindProjectInfoMapper.selectFindProjectInfoByCondition(params);
		for (int i = 0; i < page.size(); i++) {
			page.set(i, mongoTemplate.findOne(new Query(Criteria.where("_id").is(page.get(i).getId())), TeamFindProjectInfo.class));
		}
		return page;
	}

	
	@Transactional
	public int deleteFindProject(Map<String, Object> params) {
		int updateNum=0;
		try {
			updateNum=teamFindProjectInfoMapper.deleteFindProject(params);
			if(updateNum > 0){
				List<Integer> list = (List<Integer>)params.get("ids");  
				for(Integer id: list){
					Update update = new Update();
					update.set("id", id);
					update.set("utime",params.get("utime"));
					update.set("isValid", 1);
					mongoTemplate.updateFirst(new Query(Criteria.where("_id").is(id)),  update, TeamFindProjectInfo.class);
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return updateNum;
	}

	
}
